package demo;


public class Test3 {
    public static void main(String[] args) {
        //开发验证码程序

        System.out.println(getCode(4));
        System.out.println(getCode(6));
    }
    public static String getCode(int n){
        String code = "";


        for (int i = 0; i < n; i++) {
            //为当前随机生成一个数字、大写字母、小写字母 0/数字 1/小写字母 2/大写字母
            int type = (int) (Math.random() *3);

            switch (type)
            {
                case 0:
                    //0-9
                    int num = (int) (Math.random() * 10);
                    code += num;
                    break;
                case 1:
                    //a-z
                    int num1 = (int) (Math.random() * 26);
                    char ch = (char) (num1 + 'a');//'a' 97
                    code += ch;
                    break;
                case 2:
                    //A-Z
                    int num2 = (int) (Math.random() * 26);
                    code += (char) (num2 + 'A');//'A' 65
                    break;
            }
        }
        return code;
    }
}
